<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：User.class.php
 * @类功能: 用户控制器
 * @开发者: 陈旭林
 * @开发时间： 2017-09-01
 * @版本：version 1.0
 */

namespace app\partner\controller;

use app\common\cache\agent\IAgentAdminCache;
use app\common\cache\cooperate\ICooperateAdminCache;
use app\common\controller\PartnerBaseController;
use app\common\enum\AgentAdminStatus;
use app\common\enum\AppType;
use app\common\enum\IsDelete;
use app\common\enum\Source;
use app\common\ext\IDb;
use app\common\ext\ISms;

class User extends PartnerBaseController {

    /**
     * @功能：用户注册
     * @开发者：cxl
     */
    private function reg(){
        //接收参数
        $user_phone = self::get_data('phone',self::PHONE,"请输入正确的手机号码！");
        $user_pwd = self::get_data('pwd',self::NOT_EMPTY,"请输入密码！");
        $user_verification = self::get_data('verification',self::NOT_EMPTY,"请输入短信验证码！");

        //判断验证码是否正确
        $verify_list = ISms::getCode(AppType::HZ,$user_phone);
        if (!in_array($user_verification,$verify_list)) {
            self::set_code(self::WARNING_0001);
            self::set_msg("无效的验证码！");
            self::send();
        }

        //判断手机号是否已经存在
        $user_where['phone'] = $user_phone;
        $user_count = IDb::getInstance("cooperate_admin")->setDbWhere($user_where)->con();
        if($user_count > 0){
            self::set_code(self::WARNING_0002);
            self::set_msg("亲，您输入的手机号已经被注册了！");
            self::send();
        }

        //开始事物出来
        IDb::dbStartTrans();

        //添加用户信息
        $user_data['phone'] = $user_phone;
        $user_data['nick'] = get_string_rsa($user_phone, 3, 4) . "_" . rand(1000,9999);
        $user_data['pwd'] = md5(md5($user_pwd));
        $user_data['reg_ip'] = get_client_ip();
        $user_data['reg_time'] = get_date_time();
        $user_id = IDb::getInstance("cooperate_admin")->setDbData($user_data)->add();
        if($user_id === false){
            //回滚事物
            IDb::dbRollback();

            //返回错误
            self::set_code(self::ERR_0003);
            self::set_msg("注册失败，请联系客服！");
            self::send();
        }

        //添加公司信息
        $user_account_data['cooperate_admin_id'] = $user_id;
        $user_account_id = IDb::getInstance("cooperate_admin")->setDbData($user_account_data)->add();
        if($user_account_id === false){
            //回滚事物
            IDb::dbRollback();

            self::set_code(self::ERR_0004);
            self::set_msg("注册失败，请联系客服！");
            self::send();
        }

        //提交事物
        IDb::dbCommit();

        //判断手机号是否已经存在
        $user_info = ICooperateAdminCache::getInfo($user_id);
        if(empty($user_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("取得登录信息失败！");
            self::send();
        }

        //设置身份证信息
        if(!empty($user_info['real'])){
            $user_info['real'] = get_string_rsa($user_info['real'],1);
        }

        //设置身份证信息
        if(!empty($user_info['identity'])){
            $user_info['identity'] = get_string_rsa($user_info['identity'],5,8);
        }

        //用户密码信息
        unset($user_info['pwd'],$user_info['pay_pwd']);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_values($user_info);
        self::send();
    }

    /**
     * @功能：用户登录
     * @开发者：cxl
     */
    public function login(){
        //接收参数
        $user_phone = self::get_data('phone',self::PHONE,"请输入正确的手机号码！");
        $user_pwd = self::get_data('pwd',self::NOT_EMPTY,"请输入密码！");

        //判断手机号是否已经存在
        $user_where['phone'] = $user_phone;
        $user_info = IDb::getInstance("cooperate_admin")->setDbWhere($user_where)->row();
        if(empty($user_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("亲，您输入的手机号未注册！");
            self::send();
        }

        //判断用户是否被加入黑名单
        if($user_info['is_delete'] == IsDelete::Yes){
            self::set_code(self::ERR_CHECK_0006);
            self::set_msg("亲，您的账号已被禁用，请联系客服！");
            self::send();
        }

        //判断密码是否正确
        if(md5(md5($user_pwd)) != $user_info['pwd']){
            self::set_code(self::WARNING_0003);
            self::set_msg("亲，您输入的密码错误！");
            self::send();
        }else{
            //设置身份证信息
            if(!empty($user_info['real'])){
                $user_info['real'] = get_string_rsa($user_info['real'],1);
            }

            //设置身份证信息
            if(!empty($user_info['identity'])){
                $user_info['identity'] = get_string_rsa($user_info['identity'],5,8);
            }

            //用户密码信息
            unset($user_info['pwd'],$user_info['pay_pwd']);
        }

        //修改最后登录时间
        $user_data = null;
        $user_where = null;
        $user_where['cooperate_admin_id'] = $user_info['cooperate_admin_id'];
        $user_data['login_ip'] = get_client_ip();
        $user_data['login_time'] = get_date_time();
        $user_upd = IDb::getInstance("cooperate_admin")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0004);
            self::set_msg("登录失败！");
            self::send();
        }

        //设置数据
        self::set_token('_u',$user_info['cooperate_admin_id']); //设置设备编号

        //清除用户缓存
        IAgentAdminCache::deleteInfo($user_info['cooperate_admin_id']);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value(self::SUBSCRIPT_TOKEN,self::get_token());
        self::set_values($user_info);
        self::send();
    }

    /**
     * @功能：用户短信登录
     * @开发者：cxl
     */
    public function sms_login(){
        //接收参数
        $os = self::$oid;
        $user_phone = self::get_data('user_phone',self::PHONE,"请输入正确的手机号码！");
        $user_verification = self::get_data('user_verification',self::NOT_EMPTY,"请输入短信验证码！");

        //判断验证码是否正确
        $verify_list = ISms::getCode(AppType::YH,$user_phone);
        if (!in_array($user_verification,$verify_list)) {
            self::set_code(self::WARNING_0001);
            self::set_msg("亲，您输入的短信验证码错误！");
            self::send();
        }

        //判断手机号是否已经存在
        $user_where['phone'] = $user_phone;
        $user_info = IDb::getInstance("cooperate_admin")->setDbWhere($user_where)->row();
        if($user_info === false){
            self::set_code(self::WARNING_0002);
            self::set_msg("短信登录失败，请联系客服！");
            self::send();
        }

        //判断用户是否存在
        if(empty($user_info)){

            //添加用户信息
            $user_data['phone'] = $user_phone;
            $user_data['nick'] = get_string_rsa($user_phone, 3, 4) . "_" . rand(1000,9999);
            $user_data['cooperate_admin_status'] = AgentAdminStatus::normal;
            $user_data['reg_ip'] = get_client_ip();
            $user_data['reg_time'] = get_date_time();
            $user_id = IDb::getInstance("cooperate_admin")->setDbData($user_data)->add();
            if($user_id === false){
                self::set_code(self::WARNING_0003);
                self::set_msg("短信登录失败，请联系客服！");
                self::send();
            }
        }else{
            $user_id = $user_info['cooperate_admin_id'];
        }

        //修改最后登录时间
        $user_data = null;
        $user_where = null;
        $user_where['cooperate_admin_id'] = array("eq",$user_id);
        $user_data['login_ip'] = get_client_ip();
        $user_data['login_time'] = get_date_time();
        $user_upd = IDb::getInstance("cooperate_admin")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0004);
            self::set_msg("登录失败！");
            self::send();
        }

        //清除用户缓存
        ICooperateAdminCache::deleteInfo($user_id);

        //取得用户信息
        $user_info = ICooperateAdminCache::getInfo($user_id);

        //判断用户是否被加入黑名单
        if($user_info['cooperate_admin_status'] == AgentAdminStatus::blacklist){
            self::set_code(self::ERR_CHECK_0006);
            self::set_msg("亲，您的账号已被禁用，请联系客服！");
            self::send();
        }

        //设置用户信息
        if(!empty($user_info['real'])){ $user_info['real'] = get_string_rsa($user_info['real'],1); }//设置身份证信息
        if(!empty($user_info['identity'])){ $user_info['identity'] = get_string_rsa($user_info['identity'],5,8); }//设置身份证信息

        //用户密码信息
        unset($user_info['pwd'],$user_info['pay_pwd']);

        //设置数据
        $this->set_token('_u',$user_info['cooperate_admin_id']); //设置设备编号

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value(self::SUBSCRIPT_TOKEN,$this->get_token());
        self::set_values($user_info);
        self::send();
    }

    /**
     * @功能：用户退出
     * @开发者：陈旭林
     */
    public function logout(){
        //接收参数
        $device  = $this->get_data('device_no',self::NOT_EMPTY,"设备编号错误！");
        $os = $this->get_data('os',self::NOT_EMPTY,"系统区分错误！");
        $version  = $this->get_data('version',self::NOT_EMPTY,"系统版本错误！");

        //检测保存设备ID
        if (empty($device) || strlen($device) != 32) {
            self::set_code(self::ERR_0001);
            self::set_msg("设备编号错误！");
            self::send();
        }

        //判断系统区分是否正确
        if(!in_array($os,array(Source::Android,Source::Ios))){
            self::set_code(self::ERR_0002);
            self::set_msg("系统区分错误！");
            self::send();
        }

        //设置数据
        $this->set_token(self::USER,null); //设置用户编号
        $this->set_token(self::DEVICE,$device); //设置设备编号
        $this->set_token(self::OS,$os); //设置系统区分
        $this->set_token(self::VERSION,$version); //设置版本信息

        //返回参数
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value(self::SUBSCRIPT_TOKEN,$this->get_token());
        self::send();
    }

    /**
     * @功能：取得登录用户信息
     * @开发者：cxl
     */
    public function login_info(){
        //接收参数
        $user_info = self::$info;

        //设置身份证信息
        if(!empty($user_info['real'])){
            $user_info['real'] = get_string_rsa($user_info['real'],1);
        }

        //设置身份证信息
        if(!empty($user_info['identity'])){
            $user_info['identity'] = get_string_rsa($user_info['identity'],5,8);
        }

        //用户密码信息
        unset($user_info['pwd'],$user_info['pay_pwd']);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_values($user_info);
        self::send();
    }

    /**
     * @功能：设置用户昵称
     * @开发者：cxl
     */
    public function set_field(){
        //接收参数
        $user_id = self::$uid;
        $field  = self::get_data("field",self::NOT_EMPTY,"请输入修改字段！");
        $value  = self::get_data("value");

        //判断是否输入
        if(empty($value)){
            self::set_code(self::WARNING_0001);
            self::set_msg("请输入用户信息！");
            self::send();
        }

        //判断字段是否正确
        $field = strtolower($field);
        $field_array = array("nick","avatar");
        if(!in_array($field,$field_array)){
            self::set_code(self::WARNING_0002);
            self::set_msg("修改用户信息错误，请联系客服！");
            self::send();
        }

        //设置用户昵称
        $user_where['cooperate_admin_id'] = $user_id;
        $user_data[$field] = $value;
        $user_upd = IDb::getInstance("cooperate_admin")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0003);
            self::set_msg("修改用户昵称失败！");
            self::send();
        }

        //删除缓存
        ICooperateAdminCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：修改密码
     * @开发者：cxl
     */
    public function upd_pwd(){
        //接收参数
        $user_id = self::$uid;
        $user_info = self::$info;
        $user_verification = self::get_data('user_verification',self::NOT_EMPTY,"请输入短信验证码！");
        $new_pwd = self::get_data('new_pwd',self::NOT_EMPTY,"请输入新密码！");

        //判断验证码是否正确
        $verify_list = ISms::getCode(AppType::HZ,$user_info['phone']);
        if (!in_array($user_verification,$verify_list)) {
            self::set_code(self::WARNING_0001);
            self::set_msg("亲，您输入的短信验证码错误！");
            self::send();
        }

        //整理用户信息
        $user_where['cooperate_admin_id'] = $user_id;
        $user_data['pwd'] = md5(md5($new_pwd));
        $user_upd = IDb::getInstance("cooperate_admin")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::ERR_0002);
            self::set_msg("修改用户密码失败，请联系客服！");
            self::send();
        }

        //删除缓存
        ICooperateAdminCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：找回密码
     * @开发者：cxl
     */
    public function find_pwd(){
        //接收参数
        $user_phone = self::get_data('user_phone',self::NOT_EMPTY,"请输入电话号码！");
        $verification = self::get_data('verification',self::NOT_EMPTY,"请输入验证码！");
        $new_pwd = self::get_data('new_pwd',self::NOT_EMPTY,"请输入新密码！");

        //判断验证码是否正确
        $verify_list = ISms::getCode(AppType::HZ,$user_phone);
        if (!in_array($verification,$verify_list)) {
            $this->set_code(self::WARNING_0001);
            $this->set_msg("亲，您输入的短信验证码错误！");
            $this->send();
        }

        //判断手机号是否已经存在
        $user_where = null;
        $user_where['phone'] = array("eq",$user_phone);
        $user_info = IDb::getInstance("cooperate_admin")->setDbFiled("cooperate_admin_id")->setDbWhere($user_where)->row();
        if(empty($user_info)){
            self::set_code(self::WARNING_0002);
            self::set_msg("该手机号尚未注册！");
            self::send();
        }

        //整理用户信息
        $user_where = null;
        $user_data = null;
        $user_where['cooperate_admin_id'] = $user_info['cooperate_admin_id'];
        $user_data['pwd'] = md5(md5($new_pwd));
        $user_upd = IDb::getInstance("cooperate_admin")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::ERR_0001);
            self::set_msg("修改用户密码失败，请联系客服！");
            self::send();
        }

        //删除缓存
        ICooperateAdminCache::deleteInfo($user_info['cooperate_admin_id']);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }
}
